package com.yunti.yntk.activity.record.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.yunti.yntk.R;
import com.yunti.yntk.db.AppDatabase;
import com.yunti.yntk.db.po.LocalRecord;
import com.yunti.yntk.db.LocalRecordDao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class LocalRecordFragment extends Fragment {

    int pageSize = 10;
    int pageNum = 1;

    int total = 0;

    ListView recordListView;

    List<LocalRecord> examMarkLogList = new ArrayList<>();

    RecordAdapter recordAdapter;

    TextView pageText;

    TextView preButton;

    TextView nextButton;

    LocalRecordDao localRecordDao;

    public LocalRecordFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        localRecordDao = AppDatabase.getDatabase(getContext()).localRecordDao();

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_localrecord, container, false);
        recordListView = view.findViewById(R.id.recordListView);
        pageText = view.findViewById(R.id.pageText);
        preButton = view.findViewById(R.id.preButton);
        nextButton = view.findViewById(R.id.nextButton);
        recordAdapter = new RecordAdapter(getContext(), R.layout.item_localrecord, examMarkLogList);
        recordListView.setAdapter(recordAdapter);
        preButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (pageNum > 1) {
                    pageNum--;
                    getRecord();
                }
            }
        });
        nextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int totalPageNum = total / 10 + 1;
                if (pageNum < totalPageNum) {
                    pageNum++;
                    getRecord();
                }

            }
        });
        getRecord();
        return view;
    }

    private void getRecord() {
        new Thread(() -> {
            total = localRecordDao.getCount();
            List<LocalRecord> localRecords = localRecordDao.getListByPage(pageSize,(pageNum-1) * pageSize);
            examMarkLogList.clear();
            examMarkLogList.addAll(localRecords);
            int totalPageNum = total / 10 + 1;
            getActivity().runOnUiThread(() -> {
                pageText.setText(pageNum + "/" + totalPageNum);
                recordAdapter.notifyDataSetChanged();
            });

        }).start();
    }

    class RecordAdapter extends ArrayAdapter<LocalRecord> {
        public RecordAdapter(@NonNull Context context, int resource, @NonNull List<LocalRecord> objects) {
            super(context, resource, objects);
        }


        //每个子项被滚动到屏幕内的时候会被调用
        @SuppressLint("SetTextI18n")
        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            LocalRecord localRecord = getItem(position);//得到当前项的 Fruit 实例
            //为每一个子项加载设定的布局
            @SuppressLint("ViewHolder")
            View view = LayoutInflater.from(getContext()).inflate(R.layout.item_localrecord, parent, false);
            TextView zxmcText = view.findViewById(R.id.zxmcText);
            TextView xmmcText = view.findViewById(R.id.xmmcText);
            TextView nameText = view.findViewById(R.id.nameText);
            TextView scoreText = view.findViewById(R.id.scoreText);
            TextView countText = view.findViewById(R.id.countText);
            TextView timeText = view.findViewById(R.id.timeText);
            zxmcText.setText(localRecord.getZxmc());
            xmmcText.setText(localRecord.getXmmc());
            nameText.setText(localRecord.getName());
            countText.setText(localRecord.getCount() + "");
            scoreText.setText(localRecord.getScore());

            if(localRecord.getDateField()!=null){
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String formattedNow = formatter.format(localRecord.getDateField());
                timeText.setText(formattedNow);
            }

            return view;
        }
    }


}
